package com.zenitera.bigdata.project01;

import com.zenitera.bigdata.bean.UserBehavior;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

/**
 * 样例数据
 * userId itemId categoryId behavior timestamp
 * 543462,1715,1464116,pv,1511658000
 */
public class Flink01_Project01_PV_02 {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);

        env.setParallelism(2);

        env
                .readTextFile("input/UserBehavior.csv")
                .map(line -> {
                    String[] data = line.split(",");
                    return new UserBehavior(
                            Long.valueOf(data[0]),
                            Long.valueOf(data[1]),
                            Integer.valueOf(data[2]),
                            String.valueOf(data[3]),
                            Long.valueOf(data[4])
                    );
                })
                .keyBy(UserBehavior::getBehavior)
                .process(new KeyedProcessFunction<String, UserBehavior, String>() {
                    long sum = 0;

                    @Override
                    public void processElement(UserBehavior value,
                                               Context ctx,
                                               Collector<String> out) throws Exception {
                        if ("pv".equals(value.getBehavior())) {
                            sum++;
                            out.collect("pv当前值: " + sum);
                        }

                    }
                })

                .print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
